﻿/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2010>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using MyPhotoIndex.Controls;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using MyPhotoIndex.Interfaces;
using MyPhotoIndex.Interfaces.Imaging;
using System.Drawing;
using MyPhotoIndex.Plugins.PhotoInfo.Properties;
using MyPhotoIndex.Interfaces.ImageFormatProviders;
using System.Threading;
using MyPhotoIndex.Utilities;
using MyPhotoIndex.Interfaces.Plugins;
using MyPhotoIndex.Interfaces.Tagging;

namespace MyPhotoIndex.Plugins.Magnifier
{
    class PhotoInfoForm : AutoFadingForm
    {
        private IApplicationContext m_applicationContext;
        private bool m_closing = false;
        private ReadOnlyCollection<int> m_selectedImages = null;
        private Image ms_no_image_notice = Resources.no_image_notice;

        private TableLayoutPanel tableLayoutPanel1;
        private ListView listView1;
        private ImageList imageList1;
        private System.ComponentModel.IContainer components;
        private TextBox commentsTextBox;

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PhotoInfoForm));
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.commentsTextBox = new System.Windows.Forms.TextBox();
            this.listView1 = new System.Windows.Forms.ListView();
            this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            this.tableLayoutPanel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.ColumnCount = 1;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.Controls.Add(this.commentsTextBox, 0, 0);
            this.tableLayoutPanel1.Controls.Add(this.listView1, 0, 1);
            this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 2;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 59.30233F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 40.69767F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(273, 172);
            this.tableLayoutPanel1.TabIndex = 5;
            // 
            // commentsTextBox
            // 
            this.commentsTextBox.BackColor = System.Drawing.Color.White;
            this.commentsTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.commentsTextBox.Dock = System.Windows.Forms.DockStyle.Fill;
            this.commentsTextBox.Location = new System.Drawing.Point(3, 3);
            this.commentsTextBox.Multiline = true;
            this.commentsTextBox.Name = "commentsTextBox";
            this.commentsTextBox.ReadOnly = true;
            this.commentsTextBox.Size = new System.Drawing.Size(267, 96);
            this.commentsTextBox.TabIndex = 0;
            // 
            // listView1
            // 
            this.listView1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
            this.listView1.Location = new System.Drawing.Point(3, 105);
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(267, 64);
            this.listView1.SmallImageList = this.imageList1;
            this.listView1.TabIndex = 1;
            this.listView1.UseCompatibleStateImageBehavior = false;
            this.listView1.View = System.Windows.Forms.View.SmallIcon;
            // 
            // imageList1
            // 
            this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
            this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
            this.imageList1.Images.SetKeyName(0, "tag.png");
            // 
            // PhotoInfoForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.BackColor = System.Drawing.Color.White;
            this.ClientSize = new System.Drawing.Size(273, 172);
            this.Controls.Add(this.tableLayoutPanel1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "PhotoInfoForm";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.Text = "Photo Info";
            this.tableLayoutPanel1.ResumeLayout(false);
            this.tableLayoutPanel1.PerformLayout();
            this.ResumeLayout(false);

        }

        public PhotoInfoForm(IApplicationContext applicationContext)
        {
            InitializeComponent();
            m_applicationContext = applicationContext;
        }

        public void CloseForm()
        {
            m_closing = true;
            this.Close();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            this.Visible = false;

            if (m_closing == false)
            {
                e.Cancel = true;
            }
        }

        internal void SelectionChanged(ReadOnlyCollection<int> selectedImages)
        {
            m_selectedImages = selectedImages;
            RenderDisplay();
        }

        private void RenderDisplay()
        {
            commentsTextBox.Clear();
            listView1.Items.Clear();

            if (m_selectedImages.Count > 0)
            {
                int imageId = m_selectedImages[0];
                IImageData imageData = m_applicationContext.ImageCollection[imageId];
                commentsTextBox.Text = imageData.Comment;

                foreach (int imageTagId in imageData.Tags)
                {
                    IImageTag tag = m_applicationContext.ImageTagCollection.GetTag(imageTagId);
                    listView1.Items.Add(tag.Name, 0);
                }
            }
        }
    }
}
